package com.scy.boot04test05.controller;

import com.scy.boot04test05.entity.House;
import com.scy.boot04test05.mapper.HouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HouseController {
    @Autowired
    HouseMapper houseMapper;

    @RequestMapping("/insertHouse")
    public String insert(House house){
        houseMapper.insert(house);
        return "添加成功<a href='/'>返回首页</a>";
    }
    @RequestMapping("/selectHouse")
    public String select(){
        List<House> list = houseMapper.select();
        //把list集合中的数据装金表格标签
        String html = "<table border='1'>";
        html+="<caption>房源列表</caption>";
        html+="<tr><th>id</th><th>地址</th><th>楼层</th><th>单价(元/每平米)</th><th>面积</th><th>总价</th><th>操作</th></tr>";
        for (House house :
                list) {
            html+="<tr>";
            html+="<td>"+house.getId()+"</td>";
            html+="<td>"+house.getAddress()+"</td>";
            html+="<td>"+house.getStorey()+"</td>";
            html+="<td>"+house.getHousePrices()+"</td>";
            html+="<td>"+house.getArea()+"</td>";
            html+="<td>"+(house.getHousePrices()*house.getArea())+"</td>";
            //http://localhost:8080/delete?id=xxx
            html+="<td><a href='/deleteHouse?id="+house.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html+"<br/>"+"<a href='/'>返回首页</a>";
    }
    @RequestMapping("/deleteHouse")
    public String delete(int id){
        houseMapper.delete(id);
        return "删除成功<a href='/'>返回首页</a>";
    }
    @RequestMapping("/updateHouse")
    public String update(House house){
        houseMapper.update(house);
        return "更新成功<a href='/'>返回首页</a>";
    }


}
